/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.evenmo.string;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.RecursiveTask;

/**
 * This is Description ForkJoinPool demo 分支合并线程池
 *
 * @author moyongjun
 * @date 2020/01/06
 */
public class Thread_ForkJoinPool {

    private static int[] nums=new int[1000000];
    private static final int MAX_NUM=50000;
    private static Random r=new Random();


    static{
        for (int i = 0; i < nums.length; i++) {
            nums[i]=r.nextInt(101);
        }

        long s=System.currentTimeMillis();
        int sum = Arrays.stream(nums).sum();
        long e=System.currentTimeMillis();
        System.out.println("求和结果是"+sum+"，共用时"+(e-s)+"毫秒");
    }


    static class MyForkTask extends RecursiveAction{
        private int start,end;

        public MyForkTask(int s,int e){
            this.start=s;
            this.end=e;
        }

        @Override
        protected void compute() {



            if (end-start<=MAX_NUM){

                long sum=0;

                for (int i = start; i <=end; i++) {
                    sum+=nums[i];
                }

                System.out.println("第"+start+"到"+end+"之间的和是"+sum);

            }else{
                int middle=start +(end-start)/2;

                MyForkTask forkTask1=new MyForkTask(start,middle);
                MyForkTask forkTask2=new MyForkTask(middle,end);

                forkTask1.fork();
                forkTask2.fork();
            }


        }
    }


    static class MyForkTask2 extends RecursiveTask<Long> {
        private int start,end;

        public MyForkTask2(int s,int e){
            this.start=s;
            this.end=e;
        }

        @Override
        protected Long compute() {



            if (end-start<=MAX_NUM){

                long sum=0;

                for (int i = start; i <end; i++) {
                    sum+=nums[i];
                }

                System.out.println("第"+start+"到"+end+"之间的和是"+sum);

                return sum;
            }
                int middle=start +(end-start)/2;

                MyForkTask2 forkTask1=new MyForkTask2(start,middle);
                MyForkTask2 forkTask2=new MyForkTask2(middle,end);

                forkTask1.fork();
                forkTask2.fork();

                return forkTask1.join()+forkTask2.join();//join拿到分任务的结果


        }
    }

    public static void main(String[] args) {


        ForkJoinPool forkJoinPool=new ForkJoinPool();

        MyForkTask task=new MyForkTask(0,nums.length);
        MyForkTask2 task2=new MyForkTask2(0,nums.length);
       // forkJoinPool.execute(task);
        forkJoinPool.execute(task2);
long s=System.currentTimeMillis();
        Long join = task2.join();
        long e=System.currentTimeMillis();
        System.out.println("sum="+join+"，共用时"+(e-s)+"毫秒");




//        try {
//           System.in.read();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }





    }

}
